//
//  DiscoverTopView.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/6/18.
//

#import "DiscoverTopView.h"

@interface DiscoverTopView()

@property(nonatomic,strong)UIView *bgView;
@property(nonatomic,strong)UIView *topView;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UIScrollView *topBgScrollview;
@property(nonatomic,strong)UIView *rightView;

@end

@implementation DiscoverTopView

-(instancetype)initWithFrame:(CGRect)frame andCurrentAvailableWidth:(CGFloat)currentAvailableWidth{
    self = [super initWithFrame:frame];
    if(self){
        self.currentAvailableWidth = currentAvailableWidth;
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    __weak typeof(self)self_weak_ = self;
    self.backgroundColor = UIColorHex_(0x000000,0.2);
    
    UITapGestureRecognizer *hiddenTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenClick:)];
    [self addGestureRecognizer:hiddenTap];
    
    CGFloat w80 = [DeviceUtils getAdaptedWidthForDesignWidth:80 andCurrentAvailableWidth:self.currentAvailableWidth];
    
    self.topView = [[UIView alloc]init];
    self.topView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(@0);
        make.height.mas_equalTo(@38);
        make.width.mas_equalTo(self_weak_.currentAvailableWidth-w80);
    }];
    
   
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.font = JPUIFontPingFangSCRegular(13);
    self.titleLabel.textColor = KFontGrayColor;
    [self.topView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@12);
        make.height.mas_equalTo(@18);
        make.top.mas_equalTo(@12);
        make.width.mas_equalTo(self_weak_.currentAvailableWidth-w80-12-32);
    }];
    self.titleLabel.hidden = YES;
    
    self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0,15,self.currentAvailableWidth-w80,34)];
    self.bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.bgView];
  
    self.topBgScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0,15,self.currentAvailableWidth-w80-32,34)];
    self.topBgScrollview.backgroundColor = [UIColor whiteColor];
    self.topBgScrollview.showsHorizontalScrollIndicator = NO;
    self.topBgScrollview.showsVerticalScrollIndicator = NO;
    [self addSubview:self.topBgScrollview];

    self.upDownBtn = [[UIButton alloc]init];
    self.upDownBtn.backgroundColor = [UIColor whiteColor];
    [self.upDownBtn setImage:[UIImage imageNamed:@"discover_more_down"] forState:UIControlStateNormal];
    [self.upDownBtn setImage:[UIImage imageNamed:@"discover_more_up"] forState:UIControlStateSelected];
    [self.upDownBtn addTarget:self action:@selector(upDownBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.upDownBtn.adjustsImageWhenHighlighted = NO;
    [self addSubview:self.upDownBtn];
    [self.upDownBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(@22);
        make.left.mas_equalTo(self_weak_.currentAvailableWidth-w80-10-22);
        make.top.mas_equalTo(@15);
    }];
    
    UIView *rightView = [[UIView alloc]init];
    rightView.backgroundColor = [UIColor whiteColor];
    [self addSubview:rightView];
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self_weak_.currentAvailableWidth-w80-10);
        make.height.mas_equalTo(@22);
        make.width.mas_equalTo(@10);
        make.top.mas_equalTo(@15);
    }];
    self.rightView = rightView;
    
}

-(void)refreshWidth{
    
    __weak typeof(self)self_weak_ = self;
    CGFloat w80 = [DeviceUtils getAdaptedWidthForDesignWidth:80 andCurrentAvailableWidth:self.currentAvailableWidth];
    [self.topView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self_weak_.currentAvailableWidth-w80);
    }];
    [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self_weak_.currentAvailableWidth-w80-12-32);
    }];
    self.bgView.frame = CGRectMake(0,15,self.currentAvailableWidth-w80,34);
    self.topBgScrollview.frame = CGRectMake(0,15,self.currentAvailableWidth-w80-32,34);
    [self.upDownBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self_weak_.currentAvailableWidth-w80-10-22);
    }];
    [self.rightView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self_weak_.currentAvailableWidth-w80-10);
    }];
    
    [self createBtnWithAnimated:NO];
    
}

#pragma mark - 隐藏出现的选择展开
-(void)hiddenClick:(UITapGestureRecognizer *)gesture{
    CGPoint touchPoint = [gesture locationInView:self];
    // 判断点击位置是否在UIImageView内
    if ((!CGRectContainsPoint(self.topView.frame,touchPoint))&&(!CGRectContainsPoint(self.bgView.frame,touchPoint))){
        // 点击位置不在顶部内，执行相应操作
        [self refreshDownUI];
    }
}

#pragma mark - 选中之后回到收起页面
-(void)refreshDownUI{
    self.upDownBtn.selected = NO;
    if(_updownBlock){
        _updownBlock(self.upDownBtn.selected);
    }
    [self createBtnWithAnimated:YES];
}

#pragma mark - 展开收起
-(void)upDownBtnClick{
    self.upDownBtn.selected = !self.upDownBtn.selected;
    if(_updownBlock){
        _updownBlock(self.upDownBtn.selected);
    }
    [self createBtnWithAnimated:YES];
}

-(void)setName:(NSString *)name{
    _name = name;
    self.titleLabel.text = name;
}

-(void)setArr:(NSArray *)arr{
    _arr = arr;
    [self createBtnWithAnimated:NO];
}

-(void)createBtnWithAnimated:(BOOL)isAnimated{
    [self.bgView removeAllSubviews];
    [self.topBgScrollview removeAllSubviews];
    if(self.upDownBtn.selected == YES){
        self.topBgScrollview.hidden = YES;
    }else{
        self.topBgScrollview.hidden = NO;
    }
    CGFloat w80 = [DeviceUtils getAdaptedWidthForDesignWidth:80 andCurrentAvailableWidth:self.currentAvailableWidth];
    CGFloat max_w = self.currentAvailableWidth-w80-12;
    CGFloat left = 12;
    CGFloat top = 0;
    CGFloat b_h = 0;
    CGFloat sel_left = 0;
    for(NSInteger i = 0; i < _arr.count;i++){
        DiscoverBasicCateListModel *model = _arr[i];
        UIButton *btn = [[UIButton alloc]init];
        [btn setTitle:model.name forState:UIControlStateNormal];
        [btn setTitleColor:KThemeColor forState:UIControlStateSelected];
        [btn setTitleColor:KFontGrayColor forState:UIControlStateNormal];
        btn.titleLabel.font = JPUIFontPingFangSCRegular(13);
        btn.layer.cornerRadius = 3;
        btn.layer.masksToBounds = YES;
        if(i == _sel_index){
            btn.backgroundColor = UIColorHex(0xE8FAF5);
            btn.selected = YES;
        }else{
            btn.backgroundColor = KbgColor;
            btn.selected = NO;
        }
        btn.tag = 100+i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        CGFloat btn_w = [self getStringWidthWithText:btn.titleLabel.text font:btn.titleLabel.font viewHeight:22];
        if((left+btn_w+12+6) >= max_w){
            if(self.upDownBtn.selected == YES){
                left = 12;
                top = top+22+6;
            }else{
                if(i == _sel_index){
                    sel_left = left+btn_w+12+6-max_w+12+6;
                }
            }
        }
        btn.frame = CGRectMake(left,top,btn_w+12,22);
        if(self.upDownBtn.selected == YES){
            [self.bgView addSubview:btn];
        }else{
            [self.topBgScrollview addSubview:btn];
        }
        left = left+6+btn_w+12;
        if(isAnimated == YES){
            btn.hidden = YES;
        }
    }
    b_h = top+22+10;
    
    if(isAnimated == YES){
        __weak typeof(self)weakSelf = self;
        [UIView animateWithDuration:0.25 animations:^{
            if(weakSelf.upDownBtn.selected == YES){
                weakSelf.titleLabel.hidden = NO;
                [weakSelf.topView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(@38);
                }];
                [weakSelf.upDownBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(@10);
                }];
                weakSelf.bgView.frame = CGRectMake(0,38,self.currentAvailableWidth-w80,b_h);
                
            }else{
                weakSelf.titleLabel.hidden = YES;
                [weakSelf.topView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(@49);
                }];
                [weakSelf.upDownBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(@15);
                }];
                weakSelf.bgView.frame = CGRectMake(0,15,self.currentAvailableWidth-w80,34);
                weakSelf.topBgScrollview.contentSize = CGSizeMake(left,0);
                if(sel_left > 0){
                    [weakSelf.topBgScrollview setContentOffset:CGPointMake(sel_left,0) animated:YES];
                }else{
                    [weakSelf.topBgScrollview setContentOffset:CGPointMake(0,0) animated:YES];
                }
            }
        } completion:^(BOOL finished) {
            [weakSelf showBtn];
        }];
    }else{
        if(self.upDownBtn.selected == YES){
            self.titleLabel.hidden = NO;
            [self.topView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(@38);
            }];
            [self.upDownBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(@10);
            }];
            self.bgView.frame = CGRectMake(0,38,self.currentAvailableWidth-w80,b_h);
            
        }else{
            self.titleLabel.hidden = YES;
            [self.topView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(@49);
            }];
            [self.upDownBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(@15);
            }];
            self.bgView.frame = CGRectMake(0,15,self.currentAvailableWidth-w80,34);
            self.topBgScrollview.contentSize = CGSizeMake(left,0);
            if(sel_left > 0){
                [self.topBgScrollview setContentOffset:CGPointMake(sel_left,0) animated:YES];
            }else{
                [self.topBgScrollview setContentOffset:CGPointMake(0,0) animated:YES];
            }
        }
    }
    
    [self bringSubviewToFront:self.upDownBtn];
    
    if(_arr.count > 0){
        self.upDownBtn.hidden = NO;
    }else{
        self.upDownBtn.hidden = YES;
    }
    
}

-(void)showBtn{
    for(NSInteger i = 0; i < _arr.count;i++){
        UIButton *btn = [self viewWithTag:100+i];
        btn.hidden = NO;
    }
}


-(void)btnClick:(UIButton*)button{
    NSInteger index = button.tag-100;
    NSString *userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    if(userId){
        UIButton *oldBtn = [self viewWithTag:100+_sel_index];
        oldBtn.selected = NO;
        oldBtn.backgroundColor = KbgColor;
        
        button.selected = YES;
        button.backgroundColor = UIColorHex(0xE8FAF5);
        
        _sel_index = index;
    }
    if(_clickBlock){
        _clickBlock(index);
    }
}

@end
